package com.laicunba.ext.web.controller.console.customer;

import java.util.concurrent.TimeUnit;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.laicunba.ext.web.controller.BaseController;
import com.laicunba.service.MobileMessageService;
import com.laicunba.util.CacheUtil;
import com.laicunba.util.MergeUtil;
import com.laicunba.util.StringUtil;
import com.laicunba.util.ThreadUtil;

@SuppressWarnings("unchecked")
@Controller
@RequestMapping(value = "/console/ext/customer")
public class BatchSendMobileController extends BaseController {

	@Autowired
	private MobileMessageService mobileMessageService;

	@RequestMapping(value = "/batch_send_mobile_message_form.htm")
	public ModelAndView form() {

		ModelAndView view = new ModelAndView();

		view.setViewName("batch_send_mobile_message_form.jsp");

		String count = CacheUtil.getAttribute("sys.batch.send.mobile.msg.total.count");
		if (StringUtil.isNotEmpty(count)) {
			view.setViewName("batch_send_mobile_message_progress.jsp");
		}

		return view;
	}

	@ResponseBody
	@RequestMapping(value = "/batch_send_mobile_message_progress.htm")
	public String progress() {
		JSONObject json = new JSONObject();
		json.put("finish_count", CacheUtil.getAttribute("sys.batch.send.mobile.msg.finish.count"));
		json.put("total_count", CacheUtil.getAttribute("sys.batch.send.mobile.msg.total.count"));
		return json.toJSONString();
	}

	@ResponseBody
	@RequestMapping(value = "/batch_send_mobile_message_lock_clear.htm")
	public String clearLock() {
		CacheUtil.removeAttribute("sys.batch.send.mobile.msg.total.count");
		CacheUtil.removeAttribute("sys.batch.send.mobile.msg.finish.count");
		JSONObject json = new JSONObject();
		return json.toJSONString();
	}

	@ResponseBody
	@RequestMapping(value = "/batch_send_mobile_message.htm")
	public String send(final String template, final String data) {
		JSONObject json = new JSONObject();

		String count = CacheUtil.getAttribute("sys.batch.send.mobile.msg.total.count");
		if (StringUtil.isNotEmpty(count)) {
			json.put("status", "runing");
			return json.toJSONString();
		}

		ThreadUtil.execute(new Runnable() {

			@Override
			public void run() {
				String[] dataArray = data.split("[\r\n]");

				CacheUtil.setAttribute("sys.batch.send.mobile.msg.total.count", Integer.toString(dataArray.length));

				JSONObject jsonData = null;
				for (String val : dataArray) {
					CacheUtil.incr("sys.batch.send.mobile.msg.finish.count", 3600 * 24);
					val = val.trim();
					if (StringUtil.isEmpty(val) || val.startsWith("#") || !val.startsWith("{")) {
						continue;
					}
					try {
						jsonData = (JSONObject) new JSONParser().parse(val);

						if (jsonData == null) {
							continue;
						}

						if (!jsonData.containsKey("mobile")) {
							continue;
						}
						TimeUnit.MILLISECONDS.sleep(100);
						String msg = MergeUtil.simpleMerge(template, jsonData);
						mobileMessageService.sendMsgAsync(jsonData.get("mobile").toString(), msg);
					} catch (Exception e) {
					}
				}

				CacheUtil.removeAttribute("sys.batch.send.mobile.msg.total.count");
				CacheUtil.removeAttribute("sys.batch.send.mobile.msg.finish.count");

			}
		});

		json.put("status", "start");
		return json.toJSONString();
	}

}
